package 笔试.去哪儿;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * @author:谢君臣
 * @Date:2021/9/14-13:35
 * @version:1.0
 * @function:
 */
public class Main01 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(in.nextLine());
        while (n>0){
            int res;

            String _str;
            try {
                _str = in.nextLine();
            } catch (Exception e) {
                _str = null;
            }

            res = new Solution().solution(_str);
            System.out.println(String.valueOf(res));
            n--;
        }
    }
}

class Solution {
    /* Write Code Here */
    public String solution1(int[] d) {
        List<Integer> list0 = new ArrayList<>() ;
        List<Integer> list1 = new ArrayList<>() ;
        List<Integer> list2 = new ArrayList<>() ;
        for (int i = 0; i <d.length; i++) {
            if (d[i]%3==0)list0.add(d[i]);
            if (d[i]%3==1)list1.add(d[i]);
            if (d[i]%3==2)list2.add(d[i]);
        }
        //list0.sort((a, b) -> Integer.compare(a, b));
        list1.sort((a, b) -> Integer.compare(a, b));
        list2.sort((a, b) -> Integer.compare(a, b));
        int len = Math.min(list1.size(),list2.size());
        for (int i = 0; i < len ; i++) {
            list0.add(list2.get(list2.size()-1-i));
            list0.add(list1.get(list1.size()-1-i));
        }
        list0.sort((a, b) -> Integer.compare(a, b));
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < list0.size(); i++) {
            res.append(list0.get(i));
        }
        return res.reverse().toString();
    }

    public int solution(int[] arr) {
        int j = 0, max = 0;
        List<Integer> list = new ArrayList<>();
        list.add(arr[0]);
        for (int i = 1; i < arr.length; i++) {
            list.add(arr[i]);
            while (j <= i) {
                int temp = fun(new ArrayList<>(list.subList(0, list.size())));
                if (temp > max) {
                    max = temp;
                    break;
                } else {
                    list.remove(j);
                    j++;
                }
            }
        }
        return max;
    }

    private int fun(List<Integer> list) {
        int res = 0;
        list.sort((a, b) -> Integer.compare(a, b));
        for (int i = 0; i < list.size(); i++) {
            res += list.get(i) * (i + 1);
        }
        return res;
    }

    public int solution(String str) {
        int res = 0;
        List<String> list = getAllSubstring(str);
        for (String s : list) {
            if (s.length() >= 4 && (s.length() + 2) % 3 == 0 && fun1(s))
                res++;
        }
        return res;
    }

    private boolean fun1(String str) {
        int len = str.length();
        int n = (len + 2) / 3;
        int i = 1;
        while (i <= n) {
            if (!(str.charAt(i - 1) == str.charAt(2 * n - i - 1) && str.charAt(2 * n - i - 1) == str.charAt(2 * n + i - 2 - 1))) {
                return false;
            }
            i++;
        }
        return true;
    }

    private List<String> getAllSubstring(String str) {
        List<String> list = new ArrayList<>();
        if (str.length() == 0)
            return list;
        else {
            for (int i = 0; i < str.length(); i++) {
                for (int j = i + 1; j <= str.length(); j++) {
                    String res = str.substring(i, j);
                    if (res.length() >= 4) list.add(str.substring(i, j));
                }
            }
            return list;
        }
    }

}